<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      style="text-align: left"
    >
      <el-form-item label="所在区域">
        <el-input v-model="formInline.area" placeholder="所在区域"></el-input>
      </el-form-item>
      <el-form-item label="连接状态">
        <el-select v-model="formInline.connectionStatus" placeholder="连接状态">
          <el-option label="在线" value="true"></el-option>
          <el-option label="不在线" value="false"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          style="background-color: green; color: white"
          icon="el-icon-search"
          @click="list"
          >查询</el-button
        >
        <el-button style="color: gray" icon="el-icon-refresh">重置</el-button>
        <el-button style="color: green" icon="el-icon-plus" @click="add"
          >添加</el-button
        >
        <el-button style="color: red" icon="el-icon-delete" @click="del"
          >删除</el-button
        >
        <el-button style="color: orange" icon="el-icon-setting" @click="fantian"
          >修改</el-button
        >
      </el-form-item>
    </el-form>

    <el-dialog title="修改灯杆" :visible.sync="dialogFormVisible">
      <el-form
        ref="form"
        :model="form1"
        label-width="80px"
        style="width: 500px; margin: 2px auto"
      >
        <el-form-item label="灯杆编号">
          <el-input v-model="form1.poleNum"></el-input>
        </el-form-item>
        <el-form-item label="连接状态" style="width: 230px">
          <el-radio-group v-model="form1.status">
            <el-radio :label="1">在线</el-radio>
            <el-radio :label="2">不在线</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所在区域" style="width: 100px">
          <el-button type="primary" size="mini" @click="location"
            >定位</el-button
          >
        </el-form-item>
        <el-form-item label="经度">
          <el-input v-model="form1.longitude"></el-input>
        </el-form-item>
        <el-form-item label="维度">
          <el-input v-model="form1.latitude"></el-input>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="form1.useName"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="form1.phone"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="baocun">确 定</el-button>
      </div>
    </el-dialog>

    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="poleNum" label="灯杆编号" align="center">
        </el-table-column>
        <el-table-column prop="status" label="连接状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 1">在线</span>
            <span v-if="scope.row.status == 2">不在线</span>
          </template>
        </el-table-column>
        <el-table-column prop="longitude" label="经度" align="center">
        </el-table-column>
        <el-table-column prop="latitude" label="维度" align="center">
        </el-table-column>
        <el-table-column prop="useName" label="负责人" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="负责人联系方式" align="center">
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.pageIndex"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="countnum"
    >
    </el-pagination>
  </div>
</template>
<script>
import {
  GetShowListLightPoles,
  deletelightPoles,
  updlightPoles,
  GetTotaocount,
} from "@/api/iotInfo/LightPoles";
export default {
  data() {
    return {
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 0) {
          return "warning-row";
        } else if (rowIndex === 2) {
          return "success-row";
        }
        return "";
      },
      tableData: [],
      dialogFormVisible: false,
      multipleSelection: [],
      ids: {},
      form1: {
        Id: "",
        poleNum: "",
        status: "",
        longitude: window.sessionStorage.getItem("Ing"),
        latitude: window.sessionStorage.getItem("lat"),
        useName: "",
        phone: "",
      },
      formInline: {
        pageIndex: 1,
        pageSize: 2,
        connectionStatus: "",
      },
      countnum: 0,
      count: 0,
    };
  },
  created() {
    this.list();
    this.totalcount();
  },
  methods: {
    //显示
    list() {
      GetShowListLightPoles(this.formInline).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.totalCount;
      });
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.formInline.pageIndex = val;
      this.list();
    },
    totalcount() {
      GetTotaocount().then((res) => {
        this.countnum = res.data;
      });
    },
    //全选/反选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //删除
    del() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error("请选择一条数据");
        return;
      }

      this.multipleSelection.forEach((res) => {
        this.ids.id = res.id;
      });
      deletelightPoles(this.ids).then((res) => {
        if (res.data) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.list();
        } else {
          this.$message.error("删除失败");
          return;
        }
      });
    },
    //修改
    fantian() {
      if (this.multipleSelection.length != 1) {
        this.$message.error("请选择一条数据");
        return;
      }
      this.dialogFormVisible = true;
      this.multipleSelection.forEach((res) => {
        this.form1 = res;
      });
    },
    baocun() {
      updlightPoles(this.form1).then((res) => {
        if (res.data) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.list();
          this.dialogFormVisible = false;
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //跳转
    add() {
      this.$router.push("/iotInfo/AddLightPoles");
    },
    //跳转
    location() {
      this.$router.push("/iotInfo/Location");
    },
  },
};
</script>
<style>
.el-table .warning-row {
  background: rgb(230, 253, 253);
}

.el-table .success-row {
  background: #f4ebf9;
}
</style>